﻿/*****************************************************************************
 
 Use of Ext is under the Ext JS license, for more information, 
 please visit http://extjs.com/products/license.php page. 
  
 You should confirm that you have read, understood and accept ethical criteria listed. 
 If not, see <http://www.mvcnext.com/license/>.
  
 Version: 1.0.0.0
 Website: http://www.mvcnext.com/
 
 Copyright (c) Yoganand R. All rights reserved.
 For continued development and updates, visit http://yogsbreath.net
 
 ******************************************************************************/
using System;
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections.Generic;
using System.Text;
using System.Reflection;

namespace MVCnExt.Web.UI.Ext
{
    /// <summary>
    /// Basic Toolbar class. Toolbar elements can be created explicitly via their constructors, or implicitly via their xtypes. Some items also have shortcut strings for creation.
    /// </summary>   
    [Description("Basic Toolbar class. Toolbar elements can be created explicitly via their constructors, or implicitly via their xtypes. Some items also have shortcut strings for creation.")]
    public class ExtToolbar : ExtComponent
    {
        List<ExtComponent> _items = new List<ExtComponent>();
        List<ScriptReference> _references = new List<ScriptReference>();

        /// <summary>
        /// Registeres script dependencies with the page
        /// </summary>
        /// <remarks>
        /// This is dependent upon GetScriptDescriptors() being called first.
        /// Otherwise, child dependencies found within the 'items' collection
        /// won't get registered. At the moment this doesn't seem to be a problem
        /// as the .NET Framework (as well as this one) follow this pattern.
        /// 
        /// The reason for this dependency is the 'items' collection. The objects
        /// in the collection aren't instantiated the same way as a control found
        /// in the page or body templates. An object in the collection has no
        /// reference to Page or ScriptManager and it's page events aren't called.
        /// I don't think this is a problem, since the 'items' collection are objects
        /// don't begin their life-cycle until the page is loaded anyway.
        /// </remarks>
        public override List<ScriptReference> GetScriptReferences()
        {
            _references.AddRange(base.GetScriptReferences());

            return _references;
        }

        public override IList<ScriptDescriptor> GetScriptDescriptors()
        {
            IList<ScriptDescriptor> list = base.GetScriptDescriptors();

            ScriptControlDescriptorBase desc = (ScriptControlDescriptorBase)list[0];

            desc.Type = "Ext.Toolbar";

            //add items
            if (items.Count != 0)
            {
                // build items list
                StringBuilder itemList = new StringBuilder("[");

                for (int i = 0; i < items.Count; i++)
                {
                    // add comma between items
                    if (i != 0)
                    {
                        itemList.Append(",");
#if DEBUG
                        itemList.AppendLine("\t\t\t");
#endif
                    }

                    // get the item script
                    IList<ScriptDescriptor> descriptors = items[i].GetScriptDescriptors();

                    // get the items's list of script dependencies
                    List<ScriptReference> references = items[i].GetScriptReferences();

                    // make sure the script dependencies are registered
                    _references.AddRange(references);

                    // write the item's config to the list
                    itemList.Append(((ExtScriptControlDescriptor)descriptors[0]).WriteScript(ScriptType.NewOnly));
                }

                itemList.Append("]");

                desc.AddPropertyBase("items", itemList);
            }

            return list;
        }               

        #region Public Properties      

        /// <summary>
        /// List of objects to be added to the toolbar
        /// </summary>
        /// <remarks>
        /// Changed this to use ExtJsObservable to allow ExtJsMenu objects. Is that correct?
        /// </remarks>
        [NotifyParentProperty(true)]
        [PersistenceMode(PersistenceMode.InnerProperty)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public List<ExtComponent> items
        {
            get
            {
                return _items;
            }
        }

        #endregion
    }
}
